package com.example.option;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;

import org.apache.http.util.TextUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyazhou on 2016/12/26.
 */

public class Option2 {
    private HashMap<String, String[]> map = new HashMap<>();

    public Option2() {
        initData();
    }

    private void initData() {
        String[] array1 = {"131827B1AB2DCE120D91A6551DDF18A7",
                "10EE4816C838D29AF681AD48E3F52F5D",
                "BE05A002C942C5678AD57118B153C51F",
                "9DDBD7B8CA51169E453C2769291B8F71",
        "92D8CBB21C7DAEA9E3D81DF89F823CE8",
        "5E2438520D9CAFC4A126E9F5B08E9909",
        "BBEAD1D1069905548A8E84AB8F122B94"};
        map.put("EAB39CD960BDF4309F9E8CC26C7E3963", array1);
    }

    public void run() throws AVException {
        AVQuery<AVObject> query = new AVQuery<>("Botany");
        query.limit(1000);// 最多返回 1000 条结果
        List<AVObject> botanys = query.find();
        for (int i = 0; i < botanys.size(); i++) {
            AVObject object = botanys.get(i);
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String[] val = (String[]) entry.getValue();

                String v = object.getString(key);
                JSONArray array = new JSONArray();
                if (!TextUtils.isEmpty(v)) {
                    array.add(v);
                }
                for (int j = 0; j < val.length; j++) {
                    String val2 = object.getString(val[j]);
                    if (!TextUtils.isEmpty(val2)) {
                        array.add(val2);
                    }
                }
                if (array.size() != 0) {
                    object.put(key, array.toString());
                }
            }

        }
        AVObject.saveAll(botanys);
    }

}
